package top.sssd.auth.service.impl.user;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.extension.conditions.query.LambdaQueryChainWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import top.sssd.auth.service.user.UserService;
import top.sssd.auth.mapper.user.UserMapper;
import top.sssd.auth.model.user.converter.UserConverter;
import top.sssd.auth.model.user.entity.User;
import top.sssd.auth.model.user.request.UserQueryReq;
import top.sssd.auth.model.user.request.UserSaveOrUpdateReq;
import top.sssd.auth.model.user.response.UserResp;

import java.io.Serializable;
import java.util.List;
import java.util.Objects;

@Service
@RequiredArgsConstructor
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements UserService {

    @Override
    public Page<UserResp> queryPage(UserQueryReq userQueryReq) {
        Page<User> page = new Page<>(userQueryReq.getPageNum(), userQueryReq.getPageSize());
        Page<User> resultPage = commonQuery(userQueryReq).page(page);
        List<UserResp> resultList = resultPage.getRecords().parallelStream()
                .map(UserConverter.INSTANCE::entityToResp)
                .toList();
        return new Page<UserResp>()
                .setTotal(resultPage.getTotal())
                .setCurrent(resultPage.getCurrent())
                .setSize(resultPage.getSize())
                .setRecords(resultList);
    }

    @Override
    public List<UserResp> queryUserRespListByUserReq(UserQueryReq UserQueryReq) {
        List<User> list = commonQuery(UserQueryReq).list();
        return list.parallelStream()
                .map(UserConverter.INSTANCE::entityToResp)
                .toList();
    }

    private LambdaQueryChainWrapper<User> commonQuery(UserQueryReq userQueryReq) {
        return this.lambdaQuery()
                .eq(ObjectUtil.isNotEmpty(userQueryReq.getId()), User::getId, userQueryReq.getId())
                .eq(ObjectUtil.isNotEmpty(userQueryReq.getUsername()), User::getUsername, userQueryReq.getUsername())
                .eq(ObjectUtil.isNotEmpty(userQueryReq.getPassword()), User::getPassword, userQueryReq.getPassword())
                .eq(ObjectUtil.isNotEmpty(userQueryReq.getSalt()), User::getSalt, userQueryReq.getSalt())
                .eq(ObjectUtil.isNotEmpty(userQueryReq.getEmail()), User::getEmail, userQueryReq.getEmail())
                .eq(ObjectUtil.isNotEmpty(userQueryReq.getPhone()), User::getPhone, userQueryReq.getPhone())
                .eq(ObjectUtil.isNotEmpty(userQueryReq.getStatus()), User::getStatus, userQueryReq.getStatus())
                .eq(ObjectUtil.isNotEmpty(userQueryReq.getLastLoginTime()), User::getLastLoginTime, userQueryReq.getLastLoginTime())
                .orderByDesc(User::getUpdatedTime);
    }

    @Override
    public UserResp getUserRespById(Serializable id) {
        User user = this.getById(id);
        if (Objects.nonNull(user)) {
            return UserConverter.INSTANCE.entityToResp(user);
        }
        throw new RuntimeException("用户表不存在");
    }

    @Override
    @Transactional(propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
    public Boolean saveUserReq(UserSaveOrUpdateReq userSaveOrUpdateReq) {
        User user = UserConverter.INSTANCE.saveOrUpdateUserReqToEntity(userSaveOrUpdateReq);
        return this.save(user);
    }

    @Override
    @Transactional(propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
    public Boolean updateUserReq(UserSaveOrUpdateReq userSaveOrUpdateReq) {
        User user = UserConverter.INSTANCE.saveOrUpdateUserReqToEntity(userSaveOrUpdateReq);
        return this.updateById(user);
    }
}

